﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos DAL que implementa el patrón Table Data Gateway para el manejo de la entidad Departamento
''' </summary>
''' <remarks></remarks>
Public Class TDGDepartamento

    Private Shared m_Instance As TDGDepartamento = Nothing


    Public Function Insert(conn As SqlConnection, Dep_Descrip As String, DVH As Int32, Optional ByRef Dep_Codigo As Int16 = 0) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        Dim outParam_Dep_Codigo As SqlParameter


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.addDepartamento"
        command.Parameters.AddWithValue("@Dep_Descrip", Dep_Descrip)
        command.Parameters.AddWithValue("@DVH", DVH)

        outParam_Dep_Codigo = command.Parameters.Add("@Dep_Codigo", SqlDbType.SmallInt)
        outParam_Dep_Codigo.Direction = ParameterDirection.InputOutput



        iRowsAffected = command.ExecuteNonQuery()


        ' Retorno valores
        Dep_Codigo = outParam_Dep_Codigo.Value

        Return iRowsAffected

    End Function

    Public Function Update(conn As SqlConnection, Dep_Codigo As Int16, Dep_Descrip As String, DVH As Int32) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.updDepartamento"
        command.Parameters.AddWithValue("@Dep_Codigo", Dep_Codigo)
        command.Parameters.AddWithValue("@Dep_Descrip", Dep_Descrip)
        command.Parameters.AddWithValue("@DVH", DVH)


        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Delete(conn As SqlConnection, Dep_Codigo As Int16) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "dbo.delDepartamento"
        command.Parameters.AddWithValue("@Dep_Codigo", Dep_Codigo)

        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Find(conn As SqlConnection, Dep_Codigo As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getDepartamento"
        command.Parameters.AddWithValue("@Dep_Codigo", Dep_Codigo)


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Function FindAll(conn As SqlConnection) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getDepartamento_All"


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Shared ReadOnly Property Instance()
        Get
            If (m_Instance Is Nothing) Then
                m_Instance = New TDGDepartamento()
            End If

            Return m_Instance
        End Get
    End Property

    Private Sub New()

    End Sub

End Class
